package com.devfest.wishmaster.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;

import com.devfest.wishmaster.db.DatabaseManager;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class WishList {

	@DatabaseField(id = true)
	private int id;

	@DatabaseField
	private String name;

	@DatabaseField(dataType = DataType.DATE, canBeNull = true)
	private Date dateCreated;

	@DatabaseField(dataType = DataType.DATE, canBeNull = true)
	private Date eventDate;

	@DatabaseField(foreign = true, foreignAutoRefresh = true)
	private User user;

	@ForeignCollectionField(eager = true)
	private ForeignCollection<WishItem> wishItemsDb;

	private List<WishItem> wishItems = new ArrayList<WishItem>();

	public List<WishItem> getWishItems() {
		return wishItems;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the dateCreated
	 */
	public Date getDateCreated() {
		return dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	@SuppressLint("SimpleDateFormat")
	public void setDateCreated(String dateCreated) {
		if (dateCreated.equals("")) {
			this.dateCreated = new Date(0);
			return;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		while (!dateCreated.endsWith("00")) {
			dateCreated += "0";
		}
		try {
			this.dateCreated = formatter.parse(dateCreated.trim());
		} catch (ParseException e) {
		}
	}

	/**
	 * @return the eventDate
	 */
	public Date getEventDate() {
		return eventDate;
	}

	/**
	 * @param eventDate
	 *            the eventDate to set
	 */
	@SuppressLint("SimpleDateFormat")
	public void setEventDate(String eventDate) {
		if (eventDate.equals("")) {
			this.eventDate = new Date(0);
			return;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		while (!eventDate.endsWith("00")) {
			eventDate += "0";
		}
		try {
			this.eventDate = formatter.parse(eventDate.trim());
		} catch (ParseException e) {
		}
	}

	/**
	 * @param eventDate
	 *            the eventDate to set
	 */
	public void setEventDate(Date eventDate) {
		this.eventDate = eventDate;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(int userId) {
		user = DatabaseManager.getInstance().getUserById(userId);
		if (user == null) {
			user = new User();
			user.setId(userId);
			DatabaseManager.getInstance().saveUser(user);
		}
	}

	/**
	 * @return the wishItems
	 */
	public ForeignCollection<WishItem> getWishItemsDb() {
		return wishItemsDb;
	}

	public int getCommentCount() {
		int result = 0;
		for (int i = 0; i < wishItems.size(); i++)
			result += wishItems.get(i).getComments().size();
		return result;
	}

	public int getNewCommentCount() {
		int result = 0;
		for (int i = 0; i < wishItems.size(); i++)
			result += wishItems.get(i).getNewComments().size();
		return result;
	}

	public WishList() {
	}
}